package cn.tmpan.project.interceptor;

import cn.tmpan.project.common.Common;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * The type Common interceptor.
 *
 * @Author tieMinPan
 * @Desc: Description 跨域拦截器处理
 * @Date 2019 -05-08 10:01
 */
@Component
@Slf4j
public class CommonInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        /**
         *@desc axios跨域请求 需要设置请求方式 一般不采用该方法 在nginx层面解决跨域问题
         */
        response.setHeader("Access-Control-Allow-Origin", "*");
        /**
         *@desc 允许执行的跨域请求方式
         */
        response.setHeader("Access-Control-Allow-Methods", "POST,OPTIONS,GET");
        response.setHeader("Access-Control-Max-Age", "3600");
        /**
         *@desc 允许添加的头部字段
         */
        response.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, " + Common.AUTH_HEADER_ADMIN + "," + Common.AUTH_HEADER_USER);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
    }
}
